import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.utils.Timer;

import haxe.FastList;
import haxe.Timer;

import nme.Assets;
import TileModel;
import HealthBarView;
import BaseLevel;
import Kongregate;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

//import RotationText;
class CircleLineLevel extends BaseLevel
{
  
  private var _pinkMap:Array<Array<Int>>;  
  private static inline var MAX_TILE_SIZE:Int = 64;
  private static inline var MAP_COLUMNS:Int = 19;
  private static inline var MAP_ROWS:Int = 8;
  
  //tile ID s in tile sheet
  private static inline var BOX:Int = 00;
  private static inline var SKY:Int = 10;
  private static inline var PLATFORM:Int = 11;
  private static inline var MACHINE_GUN:Int = 01;
  private static inline var PISTOL:Int = 02;
  private static inline var SHOTGUN:Int = 03;
  private static inline var DOOR:Int = 13;

  private static inline var PINK:Int = 21;
  private static inline var BLUE:Int = 20;

  private static inline var PinkRadius:Float = 20.5;
  private static inline var BlueRadius:Float = 20.5;
  public var _CircleModel:CircleModel;
  public var _CircleView:PlayerCircleView;
  public var _CircleController:CircleController;
  public var _c1:CircleModel;
  public var _c1_View:CircleView;
  public var _c2:CircleModel;
  public var _c2_View:CircleView;
  public var _c3:CircleModel;
  public var _c3_View:CircleView;
  public var _c4:CircleModel;
  public var _c4_View:CircleView;
  public var _c5:CircleModel;
  public var _c5_View:CircleView;
  public var _VectorModel:VectorModel;
  public var _VectorView:VectorView;
  public var _VectorTextView:VectorTextView;
  public var _Vector2Model:VectorModel;
  public var _Vector2View:VectorView;
  public var _Vector2TextView:VectorTextView;
  public var _Vector3Model:VectorModel;
  public var _Vector3View:VectorView;
  public var _Vector3TextView:VectorTextView;
  public var _Vector4Model:VectorModel;
  public var _Vector4View:VectorView;
  public var _Vector4TextView:VectorTextView;

  public var _StartPointModel:VectorModel;
  public var _StartPointView:VectorView;
  public var _StartPointModelText:VectorTextView;
  public var _StartPointNormalModel:VectorModel;
  public var _StartPointNormalView:VectorView;
  public var _StartPointNormalModelText:VectorTextView;
  public var _EndPointModel:VectorModel;
  public var _EndPointView:VectorView;
  public var _EndPointModelText:VectorTextView;
  private var _SpriteLayer:Sprite;
  public var  WelcomeText:TextField;
  public var  LineExplation1Text:TextField;
  public var  CircleForumlaText:TextField;
  public var  ForumlaTitle:TextField;
  //Check Boundary
  public var  LineExplation2Title:TextField;
  public var  LineExplation2Text:TextField;
  public var  LineExplation3Title:TextField;
  public var  LineExplation3Text:TextField;
  public var  LineExplation4Title:TextField;
  public var  LineExplation4Text:TextField;
  public var  DotProduct1:TextField;
  public var  DotProduct2:TextField;
  public var  DotProduct3:TextField;

  public function new(stage:Dynamic){
  super(stage);
  MAP_COLUMNS = 44;
    removeChild(_staticBackgroundBitmap);
    removeChild(_backgroundBitmap);
    removeChild(_foregroundBitmap);
  _staticBackgroundBitmapData = Assets.getBitmapData("Cardboard_Background.png");
  _staticBackgroundBitmap = new Bitmap(_staticBackgroundBitmapData);

  _backgroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _backgroundBitmap = new Bitmap(_backgroundBitmapData);

  _foregroundBitmapData = new BitmapData(MAP_COLUMNS * MAX_TILE_SIZE, 
          MAP_ROWS * MAX_TILE_SIZE, true, 0);
  _foregroundBitmap = new Bitmap(_foregroundBitmapData);

  _SpriteLayer = new Sprite();
  _SpriteLayer.graphics.beginFill(0x0000000,0);
  _SpriteLayer.graphics.drawRect(0,0,MAP_COLUMNS * MAX_TILE_SIZE,MAP_ROWS * MAX_TILE_SIZE);
  _SpriteLayer.visible = true;

  _camera = new Rectangle (0, 0, stage.stageWidth, stage.stageHeight);

    addChild(_staticBackgroundBitmap);
    addChild(_backgroundBitmap);
    addChild(_foregroundBitmap);
    addChild(_SpriteLayer);
  _platformMap = 
	[
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,SKY,PLATFORM],
		  [PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM,PLATFORM]
	];
  _gameObjectMap = 
	[
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,DOOR,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,DOOR,-1,-1,-1,-1,-1,-1,-1,-1],
	    [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
	  ]  ;


  buildReserveEnemies();


  // myTimer = new Timer(12);
  //   myTimer.addEventListener("timer", OnEnter);
  //   myTimer.start();
  //Add the game bitmaps
			  
			  //Run the buildMap method to convert the
			  //map array data into a visual display
			  

      
    LineExplation1Text = new TextField();
    LineExplation1Text.htmlText = "<font size ='16'>In order to see if a line (called line1) and a circle have collided. You first to calculate the line from the center of the circle and start of the line, lets call it line2. And the Perpendicular line (of line2) called the left normal, or ln.</font>";
    LineExplation1Text.x = 150;LineExplation1Text.y = 5;
    LineExplation1Text.width = 200; LineExplation1Text.height = 170;
    LineExplation1Text.wordWrap = true;
    LineExplation1Text.border = true;
    _SpriteLayer.addChild(LineExplation1Text);


    ForumlaTitle = new TextField();
    ForumlaTitle.htmlText = "<font size ='16'>Entire Forumla: Click and Drag to highlight.</font>";
    ForumlaTitle.x = 2361;ForumlaTitle.y = 15;
    ForumlaTitle.width = 350; ForumlaTitle.height = 25;
    ForumlaTitle.wordWrap = true;
    //ForumlaTitle.border = true;
    _SpriteLayer.addChild(ForumlaTitle);
    CircleForumlaText = new TextField();
    CircleForumlaText.width = 400; CircleForumlaText.height = 390;
    CircleForumlaText.htmlText = "<font size ='16'>//_ lineSide is the side we are on.
     var _lineSide = \"\";
//a is point 1 on a line and b is point 2
    line2 = new Vector(_c1.x, _c1.y, line1.a.x, line1.a.y); 
    var dp1:Float = dotProduct(line2, line1);
    var dp2:Float = dotProduct(line2, line1.ln);
    if(dp1 &lt; -line1.m && dp1 &gt; 0 )
    {
	if(dp2 &lt; 0 )
	{
	  _lineSide = \"left\";
	}   
	else
	{
	  _lineSide = \"right\";
	}
    }
    else
    {
      _lineSide = \"\";
    }
line3 = new Vector(_c1.x, _c1.y, line1.a.x, line1.a.y); 
line4 = new Vector(_c1.x, _c1.y, line1.b.x, line1.b.y); 
    if(dp2 &gt;  -_c1.radius && _lineSide == \"left\"
    || dp2 &lt; _c1.radius && _lineSide == \"right\" ||
    line3.m &lt; tileRadius || line4.m &lt;tileRadius    )
    { 
      return _lineSide;
    }</font>";
    CircleForumlaText.x = 2336;CircleForumlaText.y = 40;

    CircleForumlaText.wordWrap = true;
    CircleForumlaText.border = true;
    _SpriteLayer.addChild(CircleForumlaText);


    LineExplation2Text = new TextField();
    LineExplation2Text.width = 200; LineExplation2Text.height = 180;
    LineExplation2Text.htmlText = "<font size ='16'>The dot product (dp1) between the line1 and  line can be used to determine if the circle is in the boundaries of the line.  If the dp1 is less than zero, but greater than -line1.m than the circle is within the boundaries of the line1.</font>";
    LineExplation2Text.x = 718;LineExplation2Text.y = 25;

    LineExplation2Text.wordWrap = true;
    LineExplation2Text.border = true;
    _SpriteLayer.addChild(LineExplation2Text);

    LineExplation2Title = new TextField();
    LineExplation2Title.htmlText = "<font size ='16'>Calculate The Boundary of the Line</font>";
    LineExplation2Title.x = 698;LineExplation2Title.y = 0;
    LineExplation2Title.width = 350; LineExplation2Title.height = 25;
    LineExplation2Title.wordWrap = true;
    _SpriteLayer.addChild(LineExplation2Title);

    LineExplation3Text = new TextField();
    LineExplation3Text.width = 200; LineExplation3Text.height = 200;
    LineExplation3Text.htmlText = "<font size ='16'>The dot product (dp2) between the line1 and its left normal will equal the distance  from the center of the circle to the line. If dp2 is less than circle radius && greater than the negative circle radius. Than the circle has collided with the line.</font>";
    LineExplation3Text.x = 1304;LineExplation3Text.y = 25;

    LineExplation3Text.wordWrap = true;
    LineExplation3Text.border = true;
    _SpriteLayer.addChild(LineExplation3Text);

    LineExplation3Title = new TextField();
    LineExplation3Title.htmlText = "<font size ='16'>Calculate The Collision of the Line</font>";
    LineExplation3Title.x = 1284;LineExplation3Title.y = 0;
    LineExplation3Title.width = 350; LineExplation3Title.height = 25;
    LineExplation3Title.wordWrap = true;
    _SpriteLayer.addChild(LineExplation3Title);

    LineExplation4Text = new TextField();
    LineExplation4Text.width = 200; LineExplation4Text.height = 240;
    LineExplation4Text.htmlText = "<font size ='16'>There is one problem with this equation.  The circle can be outside the line's  boundaries, but still collided with its edges.  But to calculate this we just do a circle collision with the circle and the edge points.\n\nPut these three equation together and you know if the circle has collided with the line.</font>";
    LineExplation4Text.x = 1880;LineExplation4Text.y = 25;

    LineExplation4Text.wordWrap = true;
    LineExplation4Text.border = true;
    _SpriteLayer.addChild(LineExplation4Text);

    LineExplation4Title = new TextField();
    LineExplation4Title.htmlText = "<font size ='16'>Calculate The Collision of the Edges</font>";
    LineExplation4Title.x = 1860;LineExplation4Title.y = 0;
    LineExplation4Title.width = 350; LineExplation4Title.height = 25;
    LineExplation4Title.wordWrap = true;
    _SpriteLayer.addChild(LineExplation4Title);

    DotProduct1 = new TextField();
    DotProduct1.width = 225; DotProduct1.height = 120;
    DotProduct1.htmlText = "";
    DotProduct1.x = 938;DotProduct1.y = 15;

    DotProduct1.wordWrap = true;
    DotProduct1.border = true;
    _SpriteLayer.addChild(DotProduct1);

    DotProduct2 = new TextField();
    DotProduct2.width = 225; DotProduct2.height = 200;
    DotProduct2.htmlText = "";
    DotProduct2.x = 1514;DotProduct2.y = 15;

    DotProduct2.wordWrap = true;
    DotProduct2.border = true;
    _SpriteLayer.addChild(DotProduct2);

    DotProduct3 = new TextField();
    DotProduct3.width = 200; DotProduct3.height = 300;
    DotProduct3.htmlText = "";
    DotProduct3.x = 2115;DotProduct3.y = 15;

    DotProduct3.wordWrap = true;
    DotProduct3.border = true;
    _SpriteLayer.addChild(DotProduct3);
    }
public override function Load(inVolume:Bool,inSound:Bool,inKongVar:CKongregate,inDifficulty:Int)
{
 super.Load(inVolume,inSound,inKongVar,inDifficulty);
 buildMap(_platformMap);
 buildMap(_gameObjectMap);
 _CircleModel = new CircleModel("main");
_CircleModel.setX = 64*3.5;_CircleModel.setY = 384;
  _CircleController = new CircleController(_CircleModel);
 _CircleView = new PlayerCircleView(_CircleModel,_CircleController,stage);

  _SpriteLayer.addChild(_CircleView);

//   _c1 = new CircleModel("Secondary");
//   _c1_View = new CircleView(_c1);
//   _SpriteLayer.addChild(_c1_View);
//   _c1.xPos = 576;_c1.yPos = 384;
  
  _VectorModel = new VectorModel(320,384, 384,256);
  _VectorView = new VectorView(_VectorModel,new Rectangle(0,0,0,0),1);
_VectorTextView = new VectorTextView(_VectorModel,new Rectangle(0,0,0,0));
      _VectorTextView.Text.text = ("line1");
      _SpriteLayer.addChild(_VectorTextView);
  _SpriteLayer.addChild(_VectorView);
_VectorModel.update(320,384, 384,256);

  _Vector2Model = new VectorModel(320,384, 384,256);
  _Vector2View = new VectorView(_Vector2Model,new Rectangle(0,0,0,0),1);
_Vector2TextView = new VectorTextView(_Vector2Model,new Rectangle(0,0,0,0));
      _Vector2TextView.Text.text = ("line1");
      _SpriteLayer.addChild(_Vector2TextView);

  _SpriteLayer.addChild(_Vector2View);
_Vector2Model.update(896,384, 960,256);

  _Vector3Model = new VectorModel(320,384, 384,256);
  _Vector3View = new VectorView(_Vector3Model,new Rectangle(0,0,0,0),1);
_Vector3TextView = new VectorTextView(_Vector3Model,new Rectangle(0,0,0,0));
      _Vector3TextView.Text.text = ("line1");
      _SpriteLayer.addChild(_Vector3TextView);

  _SpriteLayer.addChild(_Vector3View);
_Vector3Model.update(1472,384, 1536,256);


  _Vector4Model = new VectorModel(320,384, 384,256);
  _Vector4View = new VectorView(_Vector4Model,new Rectangle(0,0,0,0),1);
_Vector4TextView = new VectorTextView(_Vector4Model,new Rectangle(0,0,0,0));
      _Vector4TextView.Text.text = ("line1");
      _SpriteLayer.addChild(_Vector4TextView);

  _SpriteLayer.addChild(_Vector4View);
_Vector4Model.update(2048,384, 2112,256);

  _StartPointModel = new VectorModel(_CircleModel.xPos,_CircleModel.yPos,_VectorModel.a.x,_VectorModel.a.y);
  _StartPointView = new VectorView(_StartPointModel,new Rectangle(0,0,0,0),1);
  _StartPointNormalModel = new VectorModel();
  _StartPointNormalView = new VectorView(_StartPointNormalModel,new Rectangle(0,0,0,0));
_SpriteLayer.addChild(_StartPointView);_SpriteLayer.addChild(_StartPointNormalView);
  
      _StartPointModelText = new VectorTextView(_StartPointModel,new Rectangle(0,0,0,0));
      _StartPointModelText.Text.text = ("Line2");
      //Rotate the text
      _SpriteLayer.addChild(_StartPointModelText);

      _StartPointNormalModelText= new VectorTextView(_StartPointNormalModel,new Rectangle(0,0,0,0));
      _StartPointNormalModelText.Text.text = ("ln");
      _SpriteLayer.addChild(_StartPointNormalModelText);


  _EndPointModel = new VectorModel(_CircleModel.xPos,_CircleModel.yPos,_VectorModel.b.x,_VectorModel.b.y);
  _EndPointView = new VectorView(_EndPointModel,new Rectangle(0,0,0,0),1);
_SpriteLayer.addChild(_EndPointView);
  
      _EndPointModelText = new VectorTextView(_EndPointModel,new Rectangle(0,0,0,0));
      _EndPointModelText.Text.text = ("Line3");
      //Rotate the text
      _SpriteLayer.addChild(_EndPointModelText);
// 
//   _c2 = new CircleModel("Secondary",40);
//   _c2_View = new CircleView(_c2);
//   _SpriteLayer.addChild(_c2_View);
//   _c2.setX = 1280;_c2.setY = 384;
// 
//   _c3 = new CircleModel("Secondary",1);
//   _c3_View = new CircleView(_c3);
//   _SpriteLayer.addChild(_c3_View);
//   _c3.setX = 1380;_c3.setY = 320;
// 
//   _c4 = new CircleModel("Secondary",1);
//   _c4_View = new CircleView(_c4);
//   _SpriteLayer.addChild(_c4_View);
//   _c4.setX = 1234;_c4.setY = 167;
// 
//   _c5 = new CircleModel("Secondary",15);
//   _c5_View = new CircleView(_c5);
//   _SpriteLayer.addChild(_c5_View);
//   _c5.setX = 1485;_c5.setY = 200;
  
}

public override function checkWin():Bool
{
  if(!_CircleModel.exit)
    {return false;}
  var Door1 = new SquareModel();
  Door1.xPos = 1*64;
  Door1.yPos = 6*64;
  Door1.width = 64;
  Door1.height = 64;
  var boxSquare = new SquareModel();
  boxSquare.xPos = _CircleModel.xPos-_CircleModel.width/2;
  boxSquare.yPos = _CircleModel.yPos-_CircleModel.height/2;
  boxSquare.width = _CircleModel.width;
  boxSquare.height = _CircleModel.height;
  if(_collisionController.squareModelCollision(Door1,boxSquare))
  {
    return true;
  }
    var Door2 = new SquareModel();
  Door2.xPos = 35*64;
  Door2.yPos = 6*64;
  Door2.width = 64;
  Door2.height = 64;
  if(_collisionController.squareModelCollision(Door2,boxSquare))
  {
    return true;
  }
  return  false;
}

  public  override function UnLoad()
  {
   _SpriteLayer.removeChild(_CircleView);
  _SpriteLayer.removeChild(_VectorTextView);
  _SpriteLayer.removeChild(_VectorView);
  _SpriteLayer.removeChild(_Vector2TextView);
  _SpriteLayer.removeChild(_Vector2View);
  _SpriteLayer.removeChild(_Vector3TextView);
  _SpriteLayer.removeChild(_Vector3View);
  _SpriteLayer.removeChild(_Vector4TextView);
  _SpriteLayer.removeChild(_Vector4View);
  _SpriteLayer.removeChild(_StartPointView);_SpriteLayer.removeChild(_StartPointNormalView);
  _SpriteLayer.removeChild(_StartPointModelText);
  _SpriteLayer.removeChild(_StartPointNormalModelText);
  _SpriteLayer.removeChild(_EndPointView);
  _SpriteLayer.removeChild(_EndPointModelText);

      super.UnLoad();
      //_MainCharacterController = null;

  }
public function checkCollisionCircle(_c1:CircleModel,_c2:CircleModel,totalRadii:Float,view:CircleView)
{
	var m = Math.sqrt(Math.pow(_c1.xPos -_c2.xPos,2) +
		  Math.pow( _c1.yPos - _c2.yPos,2));   
	if(m < totalRadii)
	{
	  view.hit();
	}
	else
	{
	  view.unHit();
	}
}
public function CollisionLineCircle(_c1:CircleModel,_v1:VectorModel,_vectorView:VectorView)
{

	var start = new VectorModel(_c1.xPos,_c1.yPos,_v1.a.x,_v1.a.y);
	var end = new VectorModel(_c1.xPos,_c1.yPos,_v1.b.x,_v1.b.y);
	var dp1 =VectorMath.dotProduct(start,_v1);
	var m = _v1.m;
	var dp2 = VectorMath.dotProduct(start,_v1.ln);
	var radius = _c1.radius;
	var equal1 = (dp1 > -m && dp1 < 0 );
	var equal2 = (dp2 > -radius && dp2 < radius );
	var equal3 = (start.m < radius || end.m < radius ) ;
	if((equal1 && equal2)||equal3)
	{
	  _vectorView.Hit();
	}
	else
	{
	  _vectorView.UnHit();
	}
}
override function OnEnter(e:flash.events.Event)
{
    try
    {
      if(checkWin())
      {	
	_CircleModel.exit = false;
	UnLoad();
	this.removeEventListener(Event.ENTER_FRAME, OnEnter);
	dispatchEvent(new Event("LevelFinished"));return;
      }
      _CircleModel.exit = false;
      if(Pause == "Paused")
      {
	return;
      }	
	
_EndPointModel.update(0,0,0,0);
      if(_CircleModel.xPos < 576)
      {
      _StartPointModel.update(_CircleModel.xPos,_CircleModel.yPos,_VectorModel.a.x,_VectorModel.a.y);
      var leftNormal = _StartPointModel.ln;
      _StartPointNormalModel.update(leftNormal.a.x,leftNormal.a.y,leftNormal.b.x,leftNormal.b.y);
      //CollisionLineCircle(_CircleModel,_VectorModel,_VectorView);
      }
      else if(_CircleModel.xPos < 1152)
      {
        _StartPointModel.update(_CircleModel.xPos,_CircleModel.yPos,_Vector2Model.a.x,_Vector2Model.a.y);
	var leftNormal = _StartPointModel.ln;
	var dp1 =VectorMath.dotProduct(_StartPointModel,_Vector2Model);
	var m = _Vector2Model.m;
	_StartPointNormalModel.update(leftNormal.a.x,leftNormal.a.y,leftNormal.b.x,leftNormal.b.y);
	DotProduct1.htmlText = "<font size ='16'>dp1: " + dp1 + "\n-line1.m: " + -m 
	+"\n (dp1 &gt; -line1.m && dp1 &lt; 0 ) == "+  (dp1 > -m && dp1 < 0 ) +"</font>";
	if(dp1 > -m && dp1 < 0 )
	{
	  _Vector2View.Hit();
	}
	else
	{
	  _Vector2View.UnHit();
	}
      }
      else if(_CircleModel.xPos < 1728)
      {
        _StartPointModel.update(_CircleModel.xPos,_CircleModel.yPos,_Vector3Model.a.x,_Vector3Model.a.y);
	var leftNormal = _StartPointModel.ln;
	var dp1 =VectorMath.dotProduct(_StartPointModel,_Vector3Model);
	var m = _Vector3Model.m;
	var dp2 = VectorMath.dotProduct(_StartPointModel,_Vector3Model.ln);
	var radius = _CircleModel.radius;
	_StartPointNormalModel.update(leftNormal.a.x,leftNormal.a.y,leftNormal.b.x,leftNormal.b.y);
	DotProduct2.htmlText = "<font size ='16'>"+"dp1: " + dp1  + "\ndp2: " + dp2 +"\n-line1.m: " + -m +"\n_c1.radius: " + radius
	+"\n (dp1 &gt; -line1.m && dp1 &lt; 0 ) == "+  (dp1 > -m && dp1 < 0 ) 
	+"\n dp2 &gt;  -_c1.radius  || dp2 &lt;  _c1.radius == "+  (dp2 > -radius && dp2 < radius ) 
	+"</font>";
	if((dp1 > -m && dp1 < 0) &&  (dp2 > -radius && dp2 < radius ))
	{
	  _Vector3View.Hit();
	}
	else
	{
	  _Vector3View.UnHit();
	}
      }
      else 
      {
        _StartPointModel.update(_CircleModel.xPos,_CircleModel.yPos,_Vector4Model.a.x,_Vector4Model.a.y);
	var leftNormal = _StartPointModel.ln;
	_EndPointModel.update(_CircleModel.xPos,_CircleModel.yPos,_Vector4Model.b.x,_Vector4Model.b.y);
	var dp1 =VectorMath.dotProduct(_StartPointModel,_Vector4Model);
	var m = _Vector4Model.m;
	var dp2 = VectorMath.dotProduct(_StartPointModel,_Vector4Model.ln);
	var radius = _CircleModel.radius;
	_StartPointNormalModel.update(leftNormal.a.x,leftNormal.a.y,leftNormal.b.x,leftNormal.b.y);
	var equal1 = (dp1 > -m && dp1 < 0 );
	var equal2 = (dp2 > -radius && dp2 < radius );
	var equal3 = (_StartPointModel.m < radius || _EndPointModel.m < radius ) ;
	DotProduct3.htmlText = "<font size ='16'>"+"dp1: " + dp1  + "\ndp2: " + dp2
	+"\n-line1.m: " + -m +"\nline2.m: " + _StartPointModel.m +"\nline3.m: " + _EndPointModel.m +"\n_c1.radius: " + radius
	+"\n (dp1 &gt; -line1.m && dp1 &lt; 0 ) == "+  equal1
	+"\n dp2 &gt;  -_c1.radius  || dp2 &lt;  _c1.radius == "+  equal2
	+"\n line2.m &lt;  _c1.radius  || line3.m &lt;  _c1.radius == "+  equal3
	+"\n Collided == " + Std.string((equal1 && equal2) || equal3)
	+"</font>";
      CollisionLineCircle(_CircleModel,_Vector4Model,_Vector4View);
      }
      
      
      //Clear the stage bitmap from the previous frame so that it's
      //blank when you add the new tile positions
      _foregroundBitmapData.fillRect(_foregroundBitmapData.rect, 0);
      //Set up the camera to focus on the cat
      _camera.x = _CircleModel.xPos-Constants.flashWidth*0.5;
      _camera.y = 0;
      
      //Check the camera's game world boundaries
      //Left
      if(_camera.x < 0)
      {
	_camera.x = 0;
      }
      
      //Right
      if(_camera.x > (MAP_COLUMNS * MAX_TILE_SIZE) 
	- Constants.flashWidth)
      {
	_camera.x = (MAP_COLUMNS * MAX_TILE_SIZE) - Constants.flashWidth;
      }
      
      //Bottom
      if(_camera.y > (MAP_ROWS * MAX_TILE_SIZE) - Constants.flashHeight)
      {
	_camera.y = (MAP_ROWS * MAX_TILE_SIZE) - Constants.flashHeight;
      }
      
      //Top
      if(_camera.y < 0)
      {
	_camera.y = 0;
      }
    
      //Update the box's Model
      _CircleModel.update();
  //_VectorModel.update(_CircleModel.xPos,_CircleModel.yPos, _c1.xPos,_c1.yPos);
      //if Shoting
//       shotVector.update(0, 0,0,0);
//       	var y2 =  _CircleModel.yPos + _CircleModel.height/2;
// 	var y1 = mouseY;
// 	
// 	var x2 = _CircleModel.xPos +_CircleModel.width/2;
// 	var x1 = mouseX+_camera.x;
// 	var m = (y2 - y1 )/(x2 - x1);
// 	var b = (y2) - m*(x2);
// 	var testVector = new VectorModel();
// 	testVector.update(x2, y2,x1,y1);

// 	for(column in -1...2)
// 	{
// 	  for(row in -1...2)
// 	  {
// 	    if(_CircleModel.mapRow + row < _collisionMap.length &&
// 	       _CircleModel.mapRow + row >= 0 &&
// 	       _CircleModel.mapColumn + column < _collisionMap[0].length &&
// 	       _CircleModel.mapColumn + column >= 0)
// 	    {		
// 	      	for(model in _collisionMap[_CircleModel.mapRow + row ][_CircleModel.mapColumn + column])
// 		{
// 		  //collision
// 		}
// 	    }
// 	  }
// 	}
      //Stop the box at the stage boundaries
      //StageBoundaries.stopBitmap(_CircleModel, stage);
      maxX = MAP_COLUMNS * MAX_TILE_SIZE;
      if (_CircleModel.xPos + (_CircleModel.width/2) > maxX)
      {
	_CircleModel.setX = maxX - (_CircleModel.width/2);
	_CircleModel.vx = 0;
      }
      else if (_CircleModel.xPos-(_CircleModel.width/2) < minX)
      {
	      _CircleModel.setX = minX+(_CircleModel.width/2);
	      _CircleModel.vx = 0;
      }
      if (_CircleModel.yPos - _CircleModel.height/2 < 0)
      {
	      _CircleModel.setY =  _CircleModel.height/2;
	      _CircleModel.vy = 0;
      }
      else if (_CircleModel.yPos + _CircleModel.height/2 > Constants.flashHeight && _CircleModel.vy > 0)
      {
	      _CircleModel.setY = Constants.flashHeight - _CircleModel.height/2;
	      _CircleModel.vy = 0;
      } 
      
 
    _collisionController.platformCollisionCircleModel(_CircleModel,_platformMap,MAX_TILE_SIZE,PLATFORM);

      //Scroll the game world
      _foregroundBitmap.scrollRect = _camera;
      _backgroundBitmap.scrollRect = _camera;
      _SpriteLayer.scrollRect = _camera;

      //_gunView.scrollRect = _camera;
      //drawGunObject(_gunModel,_currentGunBitmapData, _foregroundBitmapData); 
    }
    catch(err:Dynamic)
    {
     trace(err.message);
    }
}

  public override function buildReserveEnemies()
  {
  }
}